package com.btks.water3d;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;


public class MainActivity extends BaseActivity {
    private String TAG="MainActivity-----";
    private Button jumpToMapBtn;
    private Context context;

    private VideoView videoView;
    private Button jumpToNextPage;

    private final  String videoPath="/sdcard/SuperMap/video/开机介绍.mp4";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context=this;

        setContentView(R.layout.activity_main);

        initView();
        setBtn();

        playVideo();
    }


    private void initView(){
        jumpToMapBtn=findViewById(R.id.jumpToMapBtn);
        jumpToMapBtn.setVisibility(View.GONE);
        videoView=findViewById(R.id.videoView);

    }


    private void  setBtn(){
        jumpToMapBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(context,MapAcitivty.class);
                startActivity(intent);
                finish();
            }
        });
    }

    //播放视频
    private void  playVideo(){
        String path = videoPath;
        File file = new File(path);
        if (!file.exists()) {
            Toast.makeText(context, "视频文件路径错误", Toast.LENGTH_SHORT).show();
            showJumpBtn();
            return;
        }


        videoView.setVideoPath(file.getAbsolutePath());


        Log.i(TAG, "开始播放");
        videoView.start();

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                // 在播放完毕被回调
                showJumpBtn();
            }
        });


        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {

                showJumpBtn();

                return false;
            }
        });
    }

    //显示进入按钮

    private void showJumpBtn(){
        jumpToMapBtn.setVisibility(View.VISIBLE);
    }
}